package logic

import (
	"context"
	"strconv"
	"time"
	"user-srv/appconfig"
	"user-srv/common"
	"user-srv/user"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.UserSrv.UserLogin(l.ctx, &user.UserLoginRequest{
		Mobile:   req.Mobile,
		Code:     req.Code,
		Password: req.Password,
	})
	if err != nil {
		return &types.Response{
			Code:    -1,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	token, _ := common.GetJwtToken(appconfig.AppConf1.JwtConfig.AccessSecret, time.Now().Unix(), appconfig.AppConf1.JwtConfig.AccessExpire, strconv.FormatInt(login.UserId, 10))

	return &types.Response{
		Code:    0,
		Message: "登录成功",
		Data: map[string]interface{}{
			"token": token,
		},
	}, nil
}
